@extends('layout.adminlayout')
@section('css')
	<script src="/admin/js/metisMenu.min.js"></script>
	<script src="/admin/js/custom.js"></script>
	<link href="/admin/css/area_info.css" rel="stylesheet">
	<link href="/admin/css/custom.css" rel="stylesheet">
	{{--建筑选择,负责人--}}
	<link href="/admin/css/add_manage.css" rel="stylesheet">
	<link href="/admin/css/select_building.css" rel="stylesheet">
@endsection
@section('content')
	<div class="forms">
		<div class=" form-grids row form-grids-right">
			<div class="widget-shadow " data-example-id="basic-forms">
				<div class="form-title">
					<h4>添加网关 :</h4>
				</div>
				<div class="form-body">
					<form class="form-horizontal" style="position: relative">
						{{csrf_field()}}
						<div class="form-group">
							<label for="inputEmail3" class="col-sm-1 control-label">
								网关编号 :
							</label>
							<div class="col-sm-6">
								<input type="text" class="form-control" name="gateway_id" id="inputEmail3" placeholder="网关编号必须12字符，a-zA-Z0-9!">
							</div>
						</div>
						<div class="form-group">
							<label for="inputPassword3" class="col-sm-1 control-label">
								网关名称 :
							</label>
							<div class="col-sm-6">
								<input type="text" class="form-control" name="tb_name" id="inputPassword3" placeholder="网关名称">
							</div>
						</div>
						<div class="form-group">
							<label for="inputPassword3" class="col-sm-1 control-label">
								所属类型 :
							</label>
							<div class="col-sm-6">
								<select name="model" id="model" class="form-control1">
									<option value="">请选择</option>
									@if(!null == $data)
										@foreach($data as $k=>$v)
											<option value="{{$v['t_model']}}">{{$v['t_model']}}</option>
										@endforeach
									@endif
								</select>
							</div>
						</div>
						<div class="form-group">
							<label for="inputPassword3" class="col-sm-1 control-label">
								功率 :
							</label>
							<div class="col-sm-6">
								<select name="txpower" id="txpower" class="form-control1">
									<option value="">请选择</option>
									<option value="10">10dBm</option>
									<option value="13">13dBm</option>
									<option value="17">17dBm</option>
									<option value="20">20dBm</option>
									<option value="24">24dBm</option>
									<option value="31">31dBm</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label for="inputPassword3" class="col-sm-1 control-label">
								区域 :
							</label>
							<li class="area_info">
								<div class="col-sm-6">
									<select name="area" id="area" class="form-control1">
										<option value="0">请选择区域</option>
										@if($area != null)
											@foreach($area as $v)
												<option value="{{$v['a_id']}}">{{$v['a_name']}}</option>
											@endforeach
										@endif
									</select>
								</div>
							</li>
						</div>
						<div class="form-group">
							<label for="inputPassword3" class="col-sm-1 control-label">
								建筑 :
							</label>
							<li class="area_info">
								<div class="col-sm-6">
									<input type="text" name="buildings" class="form-control" value="" placeholder="请选择">
									<input type="hidden" name="building" class="form-control" value="" placeholder="请选择">
									<dl class="col-sm-6 dv4" id="building">
									</dl>
								</div>
							</li>
						</div>
						<div class="form-group">
							<label for="inputPassword3" class="col-sm-1 control-label">
								房间 :
							</label>
							<li class="area_info">
								<div class="col-sm-6">
									<select name="room" id="rooms" class="form-control1">
										<option value="0">请选择房间</option>
									</select>
								</div>
							</li>
						</div>
						<div class="form-group">
							<label for="inputPassword3" class="col-sm-1 control-label">

							</label>
							<ul id="ul">
							</ul>
						</div>
						<div class="form-group">
							<label for="inputPassword3" class="col-sm-1 control-label">
								负责人 :
							</label>
							<div class="col-sm-5">
								<input type="text" class="form-control" name="manage" placeholder="管理员名称" id="manage">
							</div>
							<button type="button" class="btn btn-default" id="add">添加</button>
						</div>
						<div class="col-sm-offset-1">
							<button type="button" class="btn btn-default" style="margin-left: 0.38rem" id="sub">提交</button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
	<script>
        //区域建筑联动
        $('#area').change(function () {
            var areaId = $('#area').val()
            $.ajax({
                type:'post',
                url:'/admin/room/getbuild',
                async:false,
                data:{
                    "_token": $("input[name=_token]").val(),
                    'areaId':areaId,
                },
                dataType:'json',
                success:function (data) {
                    var str = '<option class="dstyle" value="">请选择建筑</option>';
                    for(var i=0;i<data.length;i++){
                        str += '<option class="dstyle" value="'+data[i]['b_id']+'">'+data[i]['b_name']+'</option>';
                    }
                    $('#building').empty();
                    $('#building').append(str);
                }
            })
        })

        //建筑房间联动
        // $('#building').change(function () {
        $('#rooms').focus(function () {
            var buildingId = $('input[name=building]').val();
            $.ajax({
                type:'post',
                url:'/admin/room/getroom',
                async:false,
                data:{
                    "_token": $("input[name=_token]").val(),
                    'buildingId':buildingId,
                },
                dataType:'json',
                success:function (data) {
                    var option = '<option value="0">请选择房间</option>';
                    for(var i=0;i<data.length;i++){
                        console.log(data[i]['r_id']);
                        option += '<option value="'+data[i]['r_id']+'">'+data[i]['r_name']+'</option>';
                    }
                    $('#rooms').empty();
                    $('#rooms').append(option);
                }
            });
        });

        //提交表单
        $('#sub').click(function () {
            var gateway_id = $('input[name=gateway_id]').val();
            var tb_name = $('input[name=tb_name]').val();
            var model = $('#model').val();
            var txpower = $('#txpower').val();
            var area = $('#area').val();
            var building = $('input[name=building]').val();
            var rooms = $('#rooms').val();
            var manage = new Array();
            $('.mng').each(function () {
                manage.push($(this).html());
            });
            if(gateway_id !='' && tb_name != '' && model != '' && txpower != '' && area != 0 && building != '' && rooms != 0){
                $.ajax({
                    type:'post',
                    url:'/admin/gateway/store',
                    async:false,
                    data:{
                        "_token": $("input[name=_token]").val(),
                        'gateway_id':gateway_id,
                        'tb_name':tb_name,
                        'model':model,
                        'txpower':txpower,
                        'area':area,
                        'building':building,
                        'room':rooms,
                        'manage':manage
                    },
                    dataType:'json',
                    success:function (data) {
                        if(data.status == '1'){
                            location.href = '/admin/gateway/index';
                        }else{
                            alert(data.msg)
						}
                    }
                })
            }else{
                alert('信息填写不完整')
            }
            return false;
        });
	</script>
@endsection
@section('lastjs')
	<script src="/admin/js/add_manage.js"></script>
	<script src="/admin/js/select_building.js"></script>
@endsection
